// Learn cc.Class:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/class/index.html
// Learn Attribute:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/reference/attributes/index.html
// Learn life-cycle callbacks:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/life-cycle-callbacks/index.html

cc.Class({
    extends: cc.Component,

    properties: {
        player:{
            default:null,
            type:cc.Node
        },
        block_prefab:{
            default:[],
            type:cc.Prefab
        },
        block_root:{
            default:null,
            type:cc.Node
        },
        current_block:{
            default:null,
            type:cc.Node
        },
        next_block:{
            default:null,
            type:cc.Node
        },
        left_org:cc.p(0,0),
        map_root:{
            default:null,
            type:cc.Node
        },
        y_ratio:0.5560472,
        checkOut:{
            default:null,
            type:cc.Node
        },
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        
    },

    // 初始化
    start () {
        // 产生第一个盒子，并且盒子是随机的
        this.current_block = cc.instantiate(this.block_prefab[Math.floor(Math.random() * 3)]);
        this.blockList
        this.block_root.addChild(this.current_block);
        // 设定第一个盒子的坐标
        this.current_block.setPosition(this.block_root.convertToNodeSpaceAR(this.left_org));
        // 把玩家的位置设定到盒子的中间
        var w_pos = this.current_block.getChildByName("mid").convertToWorldSpaceAR(cc.p(0,0));
        this.player.setPosition(this.map_root.convertToNodeSpaceAR(w_pos));
        // 产生第二个盒子
        this.next_block = this.current_block;
        // 设定层级
        this.block_zorder = -1;
        this.addNewBlock();
    },

    // 产生新的盒子
    addNewBlock () {
        this.current_block = this.next_block;
        this.next_block = cc.instantiate(this.block_prefab[Math.floor(Math.random() * 3)]);
        // 后产生的盒子层级比先前的盒子层级要低
        this.next_block.setLocalZOrder(this.block_zorder);
        this.block_zorder --;
        this.block_root.addChild(this.next_block);
        var x_distance = 200 + (Math.random() * 200);
        var y_distance = x_distance * this.y_ratio;
        var next_pos = this.current_block.getPosition();
        var player = this.player.getComponent("player");
        next_pos.x += x_distance * player.direct;
        next_pos.y += y_distance;
        this.next_block.setPosition(next_pos);
        player.setNextBlock(this.next_block);
        cc.log("children:" + this.block_root.getChildren().length);
        if (this.block_root.getChildren().length >= 20) {
            for (var i = 0; i < 10; i ++) {
                this.block_root.getChildren()[0].destroy();
                cc.log("children:" + this.block_root.getChildren().length);
            }
        }
    },

    // 移动视角
    moveMap (xOffset, yOffset) {
        var moveAction = cc.moveBy(0.5, xOffset, yOffset);
        var endAction = cc.callFunc(function(){
            this.addNewBlock();
        }.bind(this));
        var seq = cc.sequence(moveAction, endAction);
        this.map_root.runAction(seq);
    },

    // 游戏结束
    onGameOver () {
        this.checkOut.active = true;
    },

    // 游戏重新开始
    startAgain () {
        cc.director.loadScene("game_scene");
    },
    // update (dt) {},
});
